package ar.com.solidussource.howmuchmobile.activities.maps;

import android.os.Bundle;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.common.entities.Market;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;
import ar.com.solidussource.howmuchmobile.common.utils.ClusterService;
import ar.com.solidussource.howmuchmobile.common.utils.MarketCluster;
import ar.com.solidussource.howmuchmobile.maps.overlays.OverlayMap;
import ar.com.solidussource.howmuchmobile.maps.tools.MapViewWithChangeListener;
import ar.com.solidussource.howmuchmobile.services.MarketRestService;
import ar.com.solidussource.howmuchmobile.services.mocks.MarketGlobalPositioningService;
import com.google.android.maps.*;

import java.util.Iterator;
import java.util.List;

public class HowMuchMap extends MapActivity {
    private int lastZoomLevel = 17;
    private List<Market> markets;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        try{
            //Obtenemos una referencia al control MapView
            final MapViewWithChangeListener mapa = (MapViewWithChangeListener) findViewById(R.id.mapa);

            //MarketRestService marketRestService = new MarketRestService();
            //markets = marketRestService.getMarkets(1, 100);
            MarketGlobalPositioningService service = new MarketGlobalPositioningService();
            markets = service.getMarkets();

            final MapController controlMapa = mapa.getController();

            //Center MAP
            Double latitud = -34.633*1E6;
            Double longitud = -58.791*1E6;
            GeoPoint loc = new GeoPoint(latitud.intValue(), longitud.intValue());
            controlMapa.setCenter(loc);

            //Zoom Controller
            mapa.setBuiltInZoomControls(true);
            controlMapa.setZoom(lastZoomLevel);

            //mapa.setOnChangeListener(new MapViewChangeListener(this, markets));

            processOverLays(mapa, markets);

        } catch (Exception e){

        }

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public void processOverLays(MapView mapa, List<Market> markets){
         //drawing markets

         ClusterService clusterService = new ClusterService();
         List<MarketCluster> marketClusters = clusterService.generateClusters(mapa, markets);

         List<Overlay> capas = mapa.getOverlays();
         capas.clear();

         Iterator<MarketCluster> ite = marketClusters.iterator();
         while(ite.hasNext()){
            MarketCluster marketCluster = ite.next();

            OverlayMap om = new OverlayMap(marketCluster);
            capas.add(om);
         }

         mapa.postInvalidate();
    }

}

/*class MapViewChangeListener implements MapViewWithChangeListener.OnChangeListener {

    private HowMuchMap mContext;
    private List<Market> mMarkets;
    private GeoPoint lastCenter = null;

    public MapViewChangeListener(HowMuchMap context, List<Market> markets) {
        super();
        mContext = context;
        mMarkets = markets;
    }

    public void onChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter,
                         int newZoom, int oldZoom) {

        final MapController controlMapa = view.getController();
        controlMapa.setZoom(newZoom);
        mContext.processOverLays(view, mMarkets);

    }

}*/